package qnb.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qnb._common.easyui.MenuVO;
import qnb.dao.SysPowerMapper;
import qnb.entity.SysPower;
import qnb.entity.SysPowerExample;
import qnb.service.SysPowerService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huangxin
 * @date 2019/5/20-10:15
 */
@Service
public class SysPowerServiceImpl implements SysPowerService {
    @Autowired
    private SysPowerMapper sysPowerMapper;

    @Override
    public List<MenuVO> readAdminPower(){
        SysPowerExample example= new SysPowerExample();
        SysPowerExample.Criteria criteria = example.createCriteria();
        criteria.andSysPowerPidEqualTo(0);

        List<SysPower> sysPowerList = sysPowerMapper.selectByExample(example);
        List<MenuVO> menuVOList = sysPowerList.stream().map(sysPower -> new MenuVO(sysPower.getSysPowerId(),
                sysPower.getSysPowerId(), sysPower.getSysPowerName(),
                sysPower.getSysPowerStyle(), sysPower.getSysPowerUrl())).collect(Collectors.toList());

        menuVOList.forEach(menuVO -> getChildren(menuVO));
        return menuVOList;
    }


    /**
     * 设置子节点
     * @param menuVO
     */
    public void getChildren(MenuVO menuVO){
        SysPowerExample example= new SysPowerExample();
        SysPowerExample.Criteria criteria = example.createCriteria();
        //查询是否存在pid为此节点id的节点
        criteria.andSysPowerPidEqualTo(menuVO.getId());

        List<SysPower> sysPowerList = sysPowerMapper.selectByExample(example);
        //如果存在子节点
        if (sysPowerList.size()>0){
            List<MenuVO> menuVOList = sysPowerList.stream().map(sysPower -> new MenuVO(sysPower.getSysPowerId(),
                    sysPower.getSysPowerId(), sysPower.getSysPowerName(),
                    sysPower.getSysPowerStyle(), sysPower.getSysPowerUrl())).collect(Collectors.toList());
            for (MenuVO vo : menuVOList) {
                getChildren(vo);
            }
            menuVO.setChildren(menuVOList);
        }
    }


//    /**
//     * 设置子节点
//     * @param sysPower
//     */
//    public void getChildren(SysPower sysPower){
//        SysPowerExample example= new SysPowerExample();
//        SysPowerExample.Criteria criteria = example.createCriteria();
//        //查询是否存在pid为此节点id的节点
//        criteria.andSysPowerPidEqualTo(sysPower.getSysPowerId());
//
//        List<SysPower> sysPowerList = sysPowerMapper.selectByExample(example);
//        if (sysPowerList!=null){
//            for (SysPower s : sysPowerList) {
//                getChildren(s);
//            }
//            sysPower.setChildren(sysPowerList);
//        }else{
//            return ;
//        }
//    }


}
